package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/1/12 18:13
 * @Version 1.0
 **/
public class AddBinary {

    /**
     * @方法一： java内置实现--超出整数范围的时候会爆
     * @param a
     * @param b
     * @return
     */
    public String addBinary(String a, String b) {
        Long aValue = Long.parseLong(a, 2);
        Long bValue = Long.parseLong(b, 2);
        String result = Long.toBinaryString(aValue + bValue);
        return result;
    }


    /**
     * @方法二： 实现二进制的加法
     * @param a
     * @param b
     * @return
     */
    public String addBinary2(String a, String b) {
        int len = Math.min(a.length(),b.length());
        StringBuilder aValue = new StringBuilder(a).reverse();
        StringBuilder bValue = new StringBuilder(b).reverse();

        System.out.println(aValue.toString() + " " + bValue.toString());

        StringBuilder result = new StringBuilder();
        int up = 0, temp;
        for (int i = 0; i < len; i++) {
            temp = aValue.charAt(i)-'0' + bValue.charAt(i)-'0' + up;
            up = temp/2;
            temp %= 2;
            result.append((char)('0'+temp));
            System.out.println(result);
        }
        int gap = Math.abs(a.length() - b.length());
        if (a.length() > b.length()) {
            for (int i = len; i < len + gap; i++) {
                temp = aValue.charAt(i)-'0' + up;
                up = temp/2;
                temp %= 2;
                result.append((char)('0'+temp));
            }

        } else {
            for (int i = len; i < len + gap; i++) {
                temp = bValue.charAt(i)-'0' + up;
                up = temp/2;
                temp %= 2;
                result.append((char)('0'+temp));
            }
        }
        while (up != 0) {
            temp = up%2;
            up = up/2;
            result.append((char)('0'+temp));
        }
        result.reverse();
        return result.toString();
    }

    /**
     * @方法三： 使用异或结果代替二进制数字的加法结果
     * - 二进制数相加结果与异或结果相同
     * @param a
     * @param b
     * @return
     */
    public String addBinary3(String a, String b) {
        int len = Math.min(a.length(),b.length());
        StringBuilder aValue = new StringBuilder(a).reverse();
        StringBuilder bValue = new StringBuilder(b).reverse();

        System.out.println(aValue.toString() + " " + bValue.toString());

        StringBuilder result = new StringBuilder();
        int up = 0, temp;
        for (int i = 0; i < len; i++) {
            // 异或运算之后 temp的值只有0/1
            temp = (aValue.charAt(i)-'0')^(bValue.charAt(i)-'0')^up;
            if ((aValue.charAt(i)-'0')+(bValue.charAt(i)-'0')+up > 1) {
                up = 1;
            } else {
                up = 0;
            }
            result.append((char)('0'+temp));
            System.out.println(result);
        }
        int gap = Math.abs(a.length() - b.length());
        if (a.length() > b.length()) {
            for (int i = len; i < len + gap; i++) {
                temp = (aValue.charAt(i)-'0') + up;
                up = temp/2;
                temp %= 2;
                result.append((char)('0'+temp));
                System.out.println(result);
            }

        } else {
            for (int i = len; i < len + gap; i++) {
                temp = bValue.charAt(i)-'0' + up;
                up = temp/2;
                temp %= 2;
                result.append((char)('0'+temp));
                System.out.println(result);
            }
        }
        while (up != 0) {
            temp = up%2;
            up = up/2;
            result.append((char)('0'+temp));
            System.out.println(result);
        }
        result.reverse();
        System.out.println(result);
        return result.toString();
    }
}
